<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" style="text/css"
	href="../extjs4.1/resources/css/ext-all.css" />
<script type="text/javascript" src="../extjs4.1/bootstrap.js"></script>
<script type="text/javascript"
	src="../extjs4.1/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript">
	Ext.define('Calculator',{
		constructor:function(){
			//this is Calculator
			return this;
		},
		plus:function(v1,v2){
			return v1 + v2;
		},
		minus:function(v1,v2){
			return v1 - v2;
		},
		multiply:function(v1,v2){
			return v1 * v2;
		},
		divid:function(v1,v2){
			return v2 / v2;
		}
	});
	Ext.define('HEX', {
		hex : function(v1) {
			return v1.toString(16);
		}
	});
	Ext.define('BIN', {
		bin:function(v1){
			return v1.toString(2);
		}
	});
	Ext.define('OCT', {
		oct:function(v1){
			return v1.toString(8);
		}
	});
	Ext.define('NewCalculator',{
		//继承该类
		extend:'Calculator',
		//混合其他类，为其添加特殊功能
		mixins:{
			Hex:'HEX',
			Bin:'BIN',
			Oct:'OCT'
		},
		convert:function(value,type){
			switch(type){
			case 2:
				return this.bin(value);
				break;
			case 8:
				return this.oct(value);
				break;
			default:
				//call函数表示将原本属性Hex对象的hex方法交给了this来执行
				return this.mixins.Hex.hex.call(this,value);
				break;
			}
		}
	});
	
	/* var ncal = Ext.create('NewCalculator');
	alert(ncal.convert(25,2));
	alert(ncal.convert(25,8));
	alert(ncal.convert(25,16));
	alert(ncal.multiply(33,3)); */
	
	Ext.define('Convert',{
		config:{
			type:'hex'
		},
		type_num:16,
		constructor:function(config){
			//初始化config中的配置，会生成getType/setType/applyType/resetType方法
			this.initConfig(config);
			return this;
		},
		//当config中的type发生改变时，会触发applyType方法，因此可以重写该方法来完成功能
		applyType:function(type){
			alert(type);
			this.type_num = type == 'hex' ? 16 : (type == 'oct' ? 8 : 2);
			return type;
		},
		convert:function(v){
			return v.toString(this.type_num);
		}
	});
	
	/* var cv = Ext.create('Convert');
	alert(cv.convert(29));
	cv.setType('oct');
	alert(cv.convert(29));
	cv.setType('bin');
	alert(cv.convert(29)); */
	
	
	Ext.define('Convert',{
		//定义静态方法
		statics:{
			hex:function(v){
				return v.toString(16);
			},
			oct:function(v){
				return v.toString(8);
			},
			bin:function(v){
				return v.toString(2);
			}
		},
		constructor:function(config){
			return config;
		}
	});
	
	/* alert(Convert.hex(29));
	alert(Convert.oct(29));
	alert(Convert.bin(29)); */
	
	
</script>
</head>
<body>

</body>
</html>